<?php
class ControllerProjectProjectCustomer extends Controller {
	private $error = array();

	public function index() {
		$this->language->load('project/project_customer');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('project/project_customer');

		$this->getList();
	}

	public function add() {
		$this->language->load('project/project_customer');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('project/project_customer');
		// var_dump($this->request->post);exit;
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {

			$this->model_project_project_customer->addProjectCustomer($this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->response->redirect($this->url->link('project/project_customer', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}

	public function edit() {
		$this->language->load('project/project_customer');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('project/project_customer');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_project_project_customer->editVendor($this->request->get['project_customer_id'], $this->request->post);

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->response->redirect($this->url->link('project/project_customer', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}

	public function delete() {
		$this->language->load('project/project_customer');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('project/project_customer');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $project_customer_id) {
				$this->model_project_project_customer->deleteVendor($project_customer_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';

			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}

			$this->response->redirect($this->url->link('project/project_customer', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getList();
	}

	public function repair() {
		$this->language->load('project/project_customer');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('project/project_customer');

		if ($this->validateRepair()) {
			$this->model_project_project_customer->repairCategories();

			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('project/project_customer', 'token=' . $this->session->data['token'], 'SSL'));
		}

		$this->getList();
	}

	protected function getList() {
		if (isset($this->request->get['filter_company_eng_name'])) {
			$filter_company_eng_name = $this->request->get['filter_company_eng_name'];
		} else {
			$filter_company_eng_name = null;
		}

		if (isset($this->request->get['filter_company_chn_name'])) {
			$filter_company_chn_name = $this->request->get['filter_company_chn_name'];
		} else {
			$filter_company_chn_name = null;
		}

		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'name';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'DESC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$url = '';
		if (isset($this->request->get['filter_company_eng_name'])) {
			$url .= '&filter_company_eng_name=' . urlencode(html_entity_decode($this->request->get['filter_company_eng_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['column_company_chn_name'])) {
			$url .= '&column_company_chn_name=' . urlencode(html_entity_decode($this->request->get['column_company_chn_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('project/project_customer', 'token=' . $this->session->data['token'] . $url, 'SSL')
		);

		$data['add'] = $this->url->link('project/project_customer/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$data['delete'] = $this->url->link('project/project_customer/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$data['repair'] = $this->url->link('project/project_customer/repair', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$data['customers'] = array();

		$filter_data = array(
			'filter_company_eng_name'	  => $filter_company_eng_name,
			'filter_company_chn_name'	  => $filter_company_chn_name,
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_limit_admin'),
			'limit' => $this->config->get('config_limit_admin')
		);

		$category_total = $this->model_project_project_customer->getTotalCategories($filter_data);

		$results = $this->model_project_project_customer->getProjectCustomer($filter_data);
		// var_dump($results);exit;

		foreach ($results as $result) {
			$data['customers'][] = array(
				'project_customer_id' => $result['project_customer_id'],
				'company_eng_name' => $result['company_eng_name'],
				'company_chn_name' => $result['company_chn_name'],
				'contact_person_eng_name' => $result['contact_person_eng_name'],
				'contact_person_chn_name' => $result['contact_person_chn_name'],
				'contact_person_tel' => $result['contact_person_tel'],
				'contact_person_email' => $result['contact_person_email'],
<<<<<<< HEAD
<<<<<<< HEAD
				'company_address' => $result['company_address'],
=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
				'contact_person_id' => (!empty($result['data']['contact_person_id']))?$result['data']['contact_person_id']:'',
				'company_website' => $result['company_website'],
				'shareholder1_eng_name' => $result['shareholder1_eng_name'],
				'shareholder1_chn_name' => $result['shareholder1_chn_name'],
				'shareholder1_id' => (!empty($result['data']['shareholder1']))?$result['data']['shareholder1']:'',
				'shareholder2_eng_name' => $result['shareholder2_eng_name'],
				'shareholder2_chn_name' => $result['shareholder2_chn_name'],
				'shareholder2_id' => (!empty($result['data']['shareholder2']))?$result['data']['shareholder2']:'',
				'shareholder3_eng_name' => $result['shareholder3_eng_name'],
				'shareholder3_chn_name' => $result['shareholder3_chn_name'],
				'shareholder3_id' => (!empty($result['data']['shareholder3']))?$result['data']['shareholder3']:'',
				'shareholder4_eng_name' => $result['shareholder4_eng_name'],
				'shareholder4_chn_name' => $result['shareholder4_chn_name'],
				'shareholder4_id' => (!empty($result['data']['shareholder4']))?$result['data']['shareholder4']:'',
				'shareholder5_eng_name' => $result['shareholder5_eng_name'],
				'shareholder5_chn_name' => $result['shareholder5_chn_name'],
				'shareholder5_id' => (!empty($result['data']['shareholder5']))?$result['data']['shareholder5']:'',
				'status' => $result['customer_status'],

				'edit'        => $this->url->link('project/project_customer/edit', 'token=' . $this->session->data['token'] . '&project_customer_id=' . $result['project_customer_id'] . $url, 'SSL'),
				'delete'      => $this->url->link('project/project_customer/delete', 'token=' . $this->session->data['token'] . '&project_customer_id=' . $result['project_customer_id'] . $url, 'SSL')
			);
		}

		$data['heading_title'] = $this->language->get('heading_title');

		$data['text_list'] = $this->language->get('text_list');
		$data['text_no_results'] = $this->language->get('text_no_results');
		$data['text_confirm'] = $this->language->get('text_confirm');

		$data['column_name'] = $this->language->get('column_name');
		$data['column_sort_order'] = $this->language->get('column_sort_order');
		$data['column_action'] = $this->language->get('column_action');
		$data['button_filter'] = $this->language->get('button_filter');

		$data['column_company_eng_name'] = $this->language->get('column_company_eng_name');
		$data['column_company_chn_name'] = $this->language->get('column_company_chn_name');
		$data['column_contact_person_eng_name'] = $this->language->get('column_contact_person_eng_name');
		$data['column_contact_person_chn_name'] = $this->language->get('column_contact_person_chn_name');
		$data['column_contact_person_tel'] = $this->language->get('column_contact_person_tel');
		$data['column_contact_person_email'] = $this->language->get('column_contact_person_email');
<<<<<<< HEAD
<<<<<<< HEAD
		$data['column_company_address'] = $this->language->get('column_company_address');
=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
		$data['column_contact_person_id'] = $this->language->get('column_contact_person_id');
		$data['column_company_website'] = $this->language->get('column_company_website');
		$data['column_shareholder1_eng_name'] = $this->language->get('column_shareholder1_eng_name');
		$data['column_shareholder1_chn_name'] = $this->language->get('column_shareholder1_chn_name');
		$data['column_shareholder1_id'] = $this->language->get('column_shareholder1_id');

		$data['column_shareholder2_eng_name'] = $this->language->get('column_shareholder2_eng_name');
		$data['column_shareholder2_chn_name'] = $this->language->get('column_shareholder2_chn_name');
		$data['column_shareholder2_id'] = $this->language->get('column_shareholder2_id');

		$data['column_shareholder3_eng_name'] = $this->language->get('column_shareholder3_eng_name');
		$data['column_shareholder3_chn_name'] = $this->language->get('column_shareholder3_chn_name');
		$data['column_shareholder3_id'] = $this->language->get('column_shareholder3_id');

		$data['column_shareholder4_eng_name'] = $this->language->get('column_shareholder4_eng_name');
		$data['column_shareholder4_chn_name'] = $this->language->get('column_shareholder4_chn_name');
		$data['column_shareholder4_id'] = $this->language->get('column_shareholder4_id');

		$data['column_shareholder5_eng_name'] = $this->language->get('column_shareholder5_eng_name');
		$data['column_shareholder5_chn_name'] = $this->language->get('column_shareholder5_chn_name');
		$data['column_shareholder5_id'] = $this->language->get('column_shareholder5_id');

		$data['column_status'] = $this->language->get('column_status');

		$data['text_enabled'] = $this->language->get('text_enabled');
		$data['text_disabled'] = $this->language->get('text_disabled');


		$data['button_add'] = $this->language->get('button_add');
		$data['button_edit'] = $this->language->get('button_edit');
		$data['button_delete'] = $this->language->get('button_delete');
		$data['button_rebuild'] = $this->language->get('button_rebuild');


		$data['token'] = $this->session->data['token'];

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$data['success'] = '';
		}

		if (isset($this->request->post['selected'])) {
			$data['selected'] = (array)$this->request->post['selected'];
		} else {
			$data['selected'] = array();
		}

		$url = '';
		if (isset($this->request->get['filter_company_eng_name'])) {
			$url .= '&filter_company_eng_name=' . urlencode(html_entity_decode($this->request->get['filter_company_eng_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_company_chn_name'])) {
			$url .= '&filter_company_chn_name=' . urlencode(html_entity_decode($this->request->get['filter_company_chn_name'], ENT_QUOTES, 'UTF-8'));
		}

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		$data['sort_company_eng_name'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=company_eng_name' . $url, 'SSL');
		$data['sort_company_chn_name'] = $this->url->link('catalog/product', 'token=' . $this->session->data['token'] . '&sort=company_chn_name' . $url, 'SSL');
		$data['sort_sort_order'] = $this->url->link('project/project_customer', 'token=' . $this->session->data['token'] . '&sort=sort_order' . $url, 'SSL');

		$url = '';

		if (isset($this->request->get['filter_company_eng_name'])) {
			$url .= '&filter_company_eng_name=' . urlencode(html_entity_decode($this->request->get['filter_company_eng_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['filter_company_chn_name'])) {
			$url .= '&filter_company_chn_name=' . urlencode(html_entity_decode($this->request->get['filter_company_chn_name'], ENT_QUOTES, 'UTF-8'));
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $category_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_limit_admin');
		$pagination->url = $this->url->link('project/project_customer', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

		$data['pagination'] = $pagination->render();

		$data['results'] = sprintf($this->language->get('text_pagination'), ($category_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($category_total - $this->config->get('config_limit_admin'))) ? $category_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $category_total, ceil($category_total / $this->config->get('config_limit_admin')));
		$data['filter_company_eng_name'] = $filter_company_eng_name;
		$data['filter_company_chn_name'] = $filter_company_chn_name;
		$data['sort'] = $sort;
		$data['order'] = $order;

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('project/project_customer_list.tpl', $data));
	}

	protected function getForm() {
		$data['heading_title'] = $this->language->get('heading_title');

		$data['text_form'] = !isset($this->request->get['category_id']) ? $this->language->get('text_add') : $this->language->get('text_edit');
		$data['text_none'] = $this->language->get('text_none');
		$data['text_default'] = $this->language->get('text_default');
		$data['text_enabled'] = $this->language->get('text_enabled');
		$data['text_disabled'] = $this->language->get('text_disabled');

		$data['entry_status'] = $this->language->get('entry_status');

		$data['button_upload'] = $this->language->get('button_upload');
		$data['button_clear'] = $this->language->get('button_clear');
		$data['entry_company_eng_name'] = $this->language->get('entry_company_eng_name');
		$data['entry_company_chn_name'] = $this->language->get('entry_company_chn_name');
		$data['entry_contact_person_eng_name'] = $this->language->get('entry_contact_person_eng_name');
		$data['entry_contact_person_chn_name'] = $this->language->get('entry_contact_person_chn_name');
<<<<<<< HEAD
<<<<<<< HEAD
		$data['entry_company_address'] = $this->language->get('entry_company_address');
=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
		$data['entry_contact_person_tel'] = $this->language->get('entry_contact_person_tel');
		$data['entry_contact_person_email'] = $this->language->get('entry_contact_person_email');
		$data['entry_contact_person_id'] = $this->language->get('entry_contact_person_id');
		$data['entry_company_website'] = $this->language->get('entry_company_website');
		$data['entry_shareholder1_eng_name'] = $this->language->get('entry_shareholder1_eng_name');
		$data['entry_shareholder1_chn_name'] = $this->language->get('entry_shareholder1_chn_name');
		$data['entry_shareholder1_id'] = $this->language->get('entry_shareholder1_id');
		$data['entry_shareholder2_eng_name'] = $this->language->get('entry_shareholder2_eng_name');
		$data['entry_shareholder2_chn_name'] = $this->language->get('entry_shareholder2_chn_name');
		$data['entry_shareholder2_id'] = $this->language->get('entry_shareholder2_id');
		$data['entry_shareholder3_eng_name'] = $this->language->get('entry_shareholder3_eng_name');
		$data['entry_shareholder3_chn_name'] = $this->language->get('entry_shareholder3_chn_name');
		$data['entry_shareholder3_id'] = $this->language->get('entry_shareholder3_id');
		$data['entry_shareholder4_eng_name'] = $this->language->get('entry_shareholder4_eng_name');
		$data['entry_shareholder4_chn_name'] = $this->language->get('entry_shareholder4_chn_name');
		$data['entry_shareholder4_id'] = $this->language->get('entry_shareholder4_id');
		$data['entry_shareholder5_eng_name'] = $this->language->get('entry_shareholder5_eng_name');
		$data['entry_shareholder5_chn_name'] = $this->language->get('entry_shareholder5_chn_name');
		$data['entry_shareholder5_id'] = $this->language->get('entry_shareholder5_id');


		$data['entry_shareholder_share_1'] = $this->language->get('entry_shareholder_share_1');
		$data['entry_shareholder_share_2'] = $this->language->get('entry_shareholder_share_2');
		$data['entry_shareholder_share_3'] = $this->language->get('entry_shareholder_share_3');
		$data['entry_shareholder_share_4'] = $this->language->get('entry_shareholder_share_4');
		$data['entry_shareholder_share_5'] = $this->language->get('entry_shareholder_share_5');




		$data['button_save'] = $this->language->get('button_save');
		$data['button_cancel'] = $this->language->get('button_cancel');

		$data['tab_general'] = $this->language->get('tab_general');
		$data['tab_data'] = $this->language->get('tab_data');
		$data['tab_design'] = $this->language->get('tab_design');

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if (isset($this->error['shareholder_share_1'])) {
			$data['error_shareholder_share_1'] = $this->error['shareholder_share_1'];
		} else {
			$data['error_shareholder_share_1'] = null;
		}

		if (isset($this->error['shareholder_share_2'])) {
			$data['error_shareholder_share_2'] = $this->error['shareholder_share_2'];
		} else {
			$data['error_shareholder_share_2'] = null;
		}

		if (isset($this->error['shareholder_share_3'])) {
			$data['error_shareholder_share_3'] = $this->error['shareholder_share_3'];
		} else {
			$data['error_shareholder_share_3'] = null;
		}

		if (isset($this->error['shareholder_share_4'])) {
			$data['error_shareholder_share_4'] = $this->error['shareholder_share_4'];
		} else {
			$data['error_shareholder_share_4'] = null;
		}

		if (isset($this->error['shareholder_share_5'])) {
			$data['error_shareholder_share_5'] = $this->error['shareholder_share_5'];
		} else {
			$data['error_shareholder_share_5'] = null;
		}

		if (isset($this->error['name'])) {
			$data['error_name'] = $this->error['name'];
		} else {
			$data['error_name'] = array();
		}

<<<<<<< HEAD
<<<<<<< HEAD
		if (isset($this->error['company_address'])) {
			$data['error_company_address'] = $this->error['company_address'];
		} else {
			$data['error_company_address'] = null;
		}

=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
		if (isset($this->error['meta_title'])) {
			$data['error_meta_title'] = $this->error['meta_title'];
		} else {
			$data['error_meta_title'] = '';
		}

		if (isset($this->error['company_eng_name'])) {
			$data['error_company_eng_name'] = $this->error['company_eng_name'];
		} else {
			$data['error_company_eng_name'] = null;
		}

		if (isset($this->error['company_chn_name'])) {
			$data['error_company_chn_name'] = $this->error['company_chn_name'];
		} else {
			$data['error_company_chn_name'] = null;
		}

		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('project/project_customer', 'token=' . $this->session->data['token'] . $url, 'SSL')
		);

		if (!isset($this->request->get['project_customer_id'])) {
			$data['action'] = $this->url->link('project/project_customer/add', 'token=' . $this->session->data['token'] . $url, 'SSL');
			$data['project_customer_id'] = '';
		} else {
			$data['action'] = $this->url->link('project/project_customer/edit', 'token=' . $this->session->data['token'] . '&project_customer_id=' . $this->request->get['project_customer_id'] . $url, 'SSL');
			$data['project_customer_id'] = $this->request->get['project_customer_id'];

		}

		$data['cancel'] = $this->url->link('project/project_customer', 'token=' . $this->session->data['token'] . $url, 'SSL');

		if (isset($this->request->get['project_customer_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$customer_info = $this->model_project_project_customer->getpProjectCustomers($this->request->get['project_customer_id']);
		}

		$data['token'] = $this->session->data['token'];

		$this->load->model('localisation/language');

		$data['languages'] = $this->model_localisation_language->getLanguages();

		$this->load->model('project/project_progress');




		if(isset($this->request->post['contact_person_id_child'])){
			// var_dump($this->request->post);exit;
			$data['contact_person_childs'] = $this->model_project_project_progress->getProjectFiles($this->request->post['contact_person_id_child']);
		} elseif (!empty($customer_info)) {
			if(!empty($customer_info['data']['contact_person_id'])){
				$data['contact_person_childs'] = $customer_info['data']['contact_person_id'];
			}else{
				$data['contact_person_childs'] = null;
			}
		}else{
			$data['contact_person_childs'] = null;
		}

		

		if(isset($this->request->post['shareholder1_child'])){
			// var_dump($this->request->post);exit;
			$data['shareholder1_childs'] = $this->model_project_project_progress->getProjectFiles($this->request->post['shareholder1_child']);
		} elseif (!empty($customer_info)) {
			if(!empty($customer_info['data']['shareholder1'])){
				$data['shareholder1_childs'] = $customer_info['data']['shareholder1'];
			}else{
				$data['shareholder1_childs'] = null;
			}
		}else{
			$data['shareholder1_childs'] = null;
		}

		if(isset($this->request->post['shareholder2_child'])){
			// var_dump($this->request->post);exit;
			$data['shareholder2_childs'] = $this->model_project_project_progress->getProjectFiles($this->request->post['shareholder2_child']);
		} elseif (!empty($customer_info)) {
			if(!empty($customer_info['data']['shareholder2'])){
				$data['shareholder2_childs'] = $customer_info['data']['shareholder2'];
			}else{
				$data['shareholder2_childs'] = null;
			}
		}else{
			$data['shareholder2_childs'] = null;
		}

		if(isset($this->request->post['shareholder3_child'])){
			// var_dump($this->request->post);exit;
			$data['shareholder3_childs'] = $this->model_project_project_progress->getProjectFiles($this->request->post['shareholder3_child']);
		} elseif (!empty($customer_info)) {
			if(!empty($customer_info['data']['shareholder3'])){
				$data['shareholder3_childs'] = $customer_info['data']['shareholder3'];
			}else{
				$data['shareholder3_childs'] = null;
			}
		}else{
			$data['shareholder3_childs'] = null;
		}

		if(isset($this->request->post['shareholder4_child'])){
			// var_dump($this->request->post);exit;
			$data['shareholder4_childs'] = $this->model_project_project_progress->getProjectFiles($this->request->post['shareholder4_child']);
		} elseif (!empty($customer_info)) {
			if(!empty($customer_info['data']['shareholder4'])){
				$data['shareholder4_childs'] = $customer_info['data']['shareholder4'];
			}else{
				$data['shareholder4_childs'] = null;
			}
		}else{
			$data['shareholder4_childs'] = null;
		}

		if(isset($this->request->post['shareholder5_child'])){
			// var_dump($this->request->post);exit;
			$data['shareholder5_childs'] = $this->model_project_project_progress->getProjectFiles($this->request->post['shareholder5_child']);
		} elseif (!empty($customer_info)) {
			if(!empty($customer_info['data']['shareholder5'])){
				$data['shareholder5_childs'] = $customer_info['data']['shareholder5'];
			}else{
				$data['shareholder5_childs'] = null;
			}
		}else{
			$data['shareholder5_childs'] = null;
		}







		if (isset($this->request->post['company_eng_name'])) {
			$data['company_eng_name'] = $this->request->post['company_eng_name'];
		} elseif (!empty($customer_info)) {
			$data['company_eng_name'] = $customer_info['company_eng_name'];
		} else {
			$data['company_eng_name'] = '';
		}
		if (isset($this->request->post['company_chn_name'])) {
			$data['company_chn_name'] = $this->request->post['company_chn_name'];
		} elseif (!empty($customer_info)) {
			$data['company_chn_name'] = $customer_info['company_chn_name'];
		} else {
			$data['company_chn_name'] = '';
		}		

		if (isset($this->request->post['contact_person_eng_name'])) {
			$data['contact_person_eng_name'] = $this->request->post['contact_person_eng_name'];
		} elseif (!empty($customer_info)) {
			$data['contact_person_eng_name'] = $customer_info['contact_person_eng_name'];
		} else {
			$data['contact_person_eng_name'] = '';
		}

		if (isset($this->request->post['contact_person_chn_name'])) {
			$data['contact_person_chn_name'] = $this->request->post['contact_person_chn_name'];
		} elseif (!empty($customer_info)) {
			$data['contact_person_chn_name'] = $customer_info['contact_person_chn_name'];
		} else {
			$data['contact_person_chn_name'] = '';
		}

		if (isset($this->request->post['contact_person_tel'])) {
			$data['contact_person_tel'] = $this->request->post['contact_person_tel'];
		} elseif (!empty($customer_info)) {
			$data['contact_person_tel'] = $customer_info['contact_person_tel'];
		} else {
			$data['contact_person_tel'] = '';
		}

		if (isset($this->request->post['contact_person_email'])) {
			$data['contact_person_email'] = $this->request->post['contact_person_email'];
		} elseif (!empty($customer_info)) {
			$data['contact_person_email'] = $customer_info['contact_person_email'];
		} else {
			$data['contact_person_email'] = '';
		}

<<<<<<< HEAD
<<<<<<< HEAD
		if (isset($this->request->post['company_address'])) {
			$data['company_address'] = $this->request->post['company_address'];
		} elseif (!empty($customer_info)) {
			$data['company_address'] = $customer_info['company_address'];
		} else {
			$data['company_address'] = '';
		}


=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
		if (isset($this->request->post['contact_person_id'])) {
			$data['contact_person_id'] = $this->request->post['contact_person_id'];
		} elseif (!empty($customer_info)) {
			$data['contact_person_id'] = $customer_info['contact_person_id'];
		} else {
			$data['contact_person_id'] = '';
		}

		if (isset($this->request->post['company_website'])) {
			$data['company_website'] = $this->request->post['company_website'];
		} elseif (!empty($customer_info)) {
			$data['company_website'] = $customer_info['company_website'];
		} else {
			$data['company_website'] = '';
		}

		if (isset($this->request->post['shareholder1_eng_name'])) {
			$data['shareholder1_eng_name'] = $this->request->post['shareholder1_eng_name'];
		} elseif (!empty($customer_info)) {
			$data['shareholder1_eng_name'] = $customer_info['shareholder1_eng_name'];
		} else {
			$data['shareholder1_eng_name'] = '';
		}

		if (isset($this->request->post['shareholder1_chn_name'])) {
			$data['shareholder1_chn_name'] = $this->request->post['shareholder1_chn_name'];
		} elseif (!empty($customer_info)) {
			$data['shareholder1_chn_name'] = $customer_info['shareholder1_chn_name'];
		} else {
			$data['shareholder1_chn_name'] = '';
		}

		if (isset($this->request->post['shareholder1_id'])) {
			$data['shareholder1_id'] = $this->request->post['shareholder1_id'];
		} elseif (!empty($customer_info)) {
			$data['shareholder1_id'] = $customer_info['shareholder1_id'];
		} else {
			$data['shareholder1_id'] = '';
		}



		if (isset($this->request->post['shareholder2_eng_name'])) {
			$data['shareholder2_eng_name'] = $this->request->post['shareholder2_eng_name'];
		} elseif (!empty($customer_info)) {
			$data['shareholder2_eng_name'] = $customer_info['shareholder2_eng_name'];
		} else {
			$data['shareholder2_eng_name'] = '';
		}

		if (isset($this->request->post['shareholder2_chn_name'])) {
			$data['shareholder2_chn_name'] = $this->request->post['shareholder2_chn_name'];
		} elseif (!empty($customer_info)) {
			$data['shareholder2_chn_name'] = $customer_info['shareholder2_chn_name'];
		} else {
			$data['shareholder2_chn_name'] = '';
		}

		if (isset($this->request->post['shareholder2_id'])) {
			$data['shareholder2_id'] = $this->request->post['shareholder2_id'];
		} elseif (!empty($customer_info)) {
			$data['shareholder2_id'] = $customer_info['shareholder2_id'];
		} else {
			$data['shareholder2_id'] = '';
		}


		if (isset($this->request->post['shareholder3_eng_name'])) {
			$data['shareholder3_eng_name'] = $this->request->post['shareholder3_eng_name'];
		} elseif (!empty($customer_info)) {
			$data['shareholder3_eng_name'] = $customer_info['shareholder3_eng_name'];
		} else {
			$data['shareholder3_eng_name'] = '';
		}

		if (isset($this->request->post['shareholder3_chn_name'])) {
			$data['shareholder3_chn_name'] = $this->request->post['shareholder3_chn_name'];
		} elseif (!empty($customer_info)) {
			$data['shareholder3_chn_name'] = $customer_info['shareholder3_chn_name'];
		} else {
			$data['shareholder3_chn_name'] = '';
		}

		if (isset($this->request->post['shareholder3_id'])) {
			$data['shareholder3_id'] = $this->request->post['shareholder3_id'];
		} elseif (!empty($customer_info)) {
			$data['shareholder3_id'] = $customer_info['shareholder3_id'];
		} else {
			$data['shareholder3_id'] = '';
		}



		if (isset($this->request->post['shareholder4_eng_name'])) {
			$data['shareholder4_eng_name'] = $this->request->post['shareholder4_eng_name'];
		} elseif (!empty($customer_info)) {
			$data['shareholder4_eng_name'] = $customer_info['shareholder4_eng_name'];
		} else {
			$data['shareholder4_eng_name'] = '';
		}

		if (isset($this->request->post['shareholder4_chn_name'])) {
			$data['shareholder4_chn_name'] = $this->request->post['shareholder4_chn_name'];
		} elseif (!empty($customer_info)) {
			$data['shareholder4_chn_name'] = $customer_info['shareholder4_chn_name'];
		} else {
			$data['shareholder4_chn_name'] = '';
		}

		if (isset($this->request->post['shareholder4_id'])) {
			$data['shareholder4_id'] = $this->request->post['shareholder4_id'];
		} elseif (!empty($customer_info)) {
			$data['shareholder4_id'] = $customer_info['shareholder4_id'];
		} else {
			$data['shareholder4_id'] = '';
		}


		if (isset($this->request->post['shareholder5_eng_name'])) {
			$data['shareholder5_eng_name'] = $this->request->post['shareholder5_eng_name'];
		} elseif (!empty($customer_info)) {
			$data['shareholder5_eng_name'] = $customer_info['shareholder5_eng_name'];
		} else {
			$data['shareholder5_eng_name'] = '';
		}

		if (isset($this->request->post['shareholder5_chn_name'])) {
			$data['shareholder5_chn_name'] = $this->request->post['shareholder5_chn_name'];
		} elseif (!empty($customer_info)) {
			$data['shareholder5_chn_name'] = $customer_info['shareholder5_chn_name'];
		} else {
			$data['shareholder5_chn_name'] = '';
		}

		if (isset($this->request->post['shareholder5_id'])) {
			$data['shareholder5_id'] = $this->request->post['shareholder5_id'];
		} elseif (!empty($customer_info)) {
			$data['shareholder5_id'] = $customer_info['shareholder5_id'];
		} else {
			$data['shareholder5_id'] = '';
		}

		if (isset($this->request->post['shareholder_share_1'])) {
			$data['shareholder_share_1'] = $this->request->post['shareholder_share_1'];
		} elseif (!empty($customer_info)) {
			$data['shareholder_share_1'] = $customer_info['shareholder_share_1'];
		} else {
			$data['shareholder_share_1'] = '';
		}

		if (isset($this->request->post['shareholder_share_2'])) {
			$data['shareholder_share_2'] = $this->request->post['shareholder_share_2'];
		} elseif (!empty($customer_info)) {
			$data['shareholder_share_2'] = $customer_info['shareholder_share_2'];
		} else {
			$data['shareholder_share_2'] = '';
		}

		if (isset($this->request->post['shareholder_share_3'])) {
			$data['shareholder_share_3'] = $this->request->post['shareholder_share_3'];
		} elseif (!empty($customer_info)) {
			$data['shareholder_share_3'] = $customer_info['shareholder_share_3'];
		} else {
			$data['shareholder_share_3'] = '';
		}

		if (isset($this->request->post['shareholder_share_4'])) {
			$data['shareholder_share_4'] = $this->request->post['shareholder_share_4'];
		} elseif (!empty($customer_info)) {
			$data['shareholder_share_4'] = $customer_info['shareholder_share_4'];
		} else {
			$data['shareholder_share_4'] = '';
		}

		if (isset($this->request->post['shareholder_share_5'])) {
			$data['shareholder_share_5'] = $this->request->post['shareholder_share_5'];
		} elseif (!empty($customer_info)) {
			$data['shareholder_share_5'] = $customer_info['shareholder_share_5'];
		} else {
			$data['shareholder_share_5'] = '';
		}



		if (isset($this->request->post['customer_status'])) {
			$data['customer_status'] = $this->request->post['customer_status'];
		} elseif (!empty($customer_info)) {
			$data['customer_status'] = ($customer_info['customer_status'] == 1)?true:false;
		} else {
			$data['customer_status'] = true;
		}


		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('project/project_customer_form.tpl', $data));
	}

	protected function validateForm() {
		if(isset($this->request->post['project_customer_id'])){
			$project_customer_id = $this->request->post['project_customer_id'];
		}else{
			$project_customer_id = false;
		}
		$this->load->model('project/project_customer');
		if ((utf8_strlen(trim($this->request->post['company_eng_name'])) < 1) || (utf8_strlen(trim($this->request->post['company_eng_name'])) > 200)) {
			$this->error['company_eng_name'] = $this->language->get('error_company_eng_name');
		}
		if ((utf8_strlen(trim($this->request->post['company_chn_name'])) < 1) || (utf8_strlen(trim($this->request->post['company_chn_name'])) > 200)) {
			$this->error['company_chn_name'] = $this->language->get('error_company_chn_name');
		}
<<<<<<< HEAD
<<<<<<< HEAD

		if(isset($this->request->post['company_address']) && !empty($this->request->post['company_address'])){
			$company_address = $this->model_project_project_customer->checkAddress($this->request->post['company_address'],$project_customer_id);
			if($company_address){
				$this->error['company_address'] =sprintf($this->language->get('error_company_address'),$company_address);
			}
		}



=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
=======
>>>>>>> d186f0ac043b98de93b00ad49075690d01e0cbaa
		if(!empty($this->request->post['shareholder_share_1']) && !empty($this->request->post['shareholder1_chn_name']) ){
			$a = $this->model_project_project_customer->checkshare($this->request->post['shareholder1_chn_name'],$project_customer_id);
			if($a){
				$this->error['shareholder_share_1'] = sprintf($this->language->get('error_shareholder_share_1'),$a);
			}
		}
		if(!empty($this->request->post['shareholder_share_2']) && !empty($this->request->post['shareholder2_chn_name']) ){
			$a = $this->model_project_project_customer->checkshare($this->request->post['shareholder2_chn_name'],$project_customer_id);
			if($a){
				$this->error['shareholder_share_2'] = sprintf($this->language->get('error_shareholder_share_2'),$a);
			}
		}
		if(!empty($this->request->post['shareholder_share_3']) && !empty($this->request->post['shareholder3_chn_name']) ){
			$a = $this->model_project_project_customer->checkshare($this->request->post['shareholder3_chn_name'],$project_customer_id);
			if($a){
				$this->error['shareholder_share_3'] = sprintf($this->language->get('error_shareholder_share_3'),$a);
			}
		}
		if(!empty($this->request->post['shareholder_share_4']) && !empty($this->request->post['shareholder4_chn_name']) ){
			$a = $this->model_project_project_customer->checkshare($this->request->post['shareholder4_chn_name'],$project_customer_id);
			if($a){
				$this->error['shareholder_share_4'] = sprintf($this->language->get('error_shareholder_share_4'),$a);
			}
		}
		if(!empty($this->request->post['shareholder_share_5']) && !empty($this->request->post['shareholder5_chn_name']) ){
			$a = $this->model_project_project_customer->checkshare($this->request->post['shareholder5_chn_name'],$project_customer_id);
			if($a){
				$this->error['shareholder_share_5'] = sprintf($this->language->get('error_shareholder_share_5'),$a);
			}
		}
		return !$this->error;
	}

	protected function validateDelete() {
		if (!$this->user->hasPermission('modify', 'project/project_customer')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return !$this->error;
	}

	protected function validateRepair() {
		if (!$this->user->hasPermission('modify', 'project/project_customer')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return !$this->error;
	}

	public function autocomplete() {
		$json = array();

		if (isset($this->request->get['filter_company_eng_name']) || isset($this->request->get['filter_company_chn_name']) ) {
			$this->load->model('project/project_customer');
			if (isset($this->request->get['filter_company_eng_name'])) {
				$filter_company_eng_name = $this->request->get['filter_company_eng_name'];
			} else {
				$filter_company_eng_name = '';
			}

			if (isset($this->request->get['filter_company_chn_name'])) {
				$filter_company_chn_name = $this->request->get['filter_company_chn_name'];
			} else {
				$filter_company_chn_name = '';
			}

			if (isset($this->request->get['limit'])) {
				$limit = $this->request->get['limit'];
			} else {
				$limit = 5;
			}

			$filter_data = array(
				'filter_company_eng_name'  => $filter_company_eng_name,
				'filter_company_chn_name' => $filter_company_chn_name,
				'start'        => 0,
				'limit'        => $limit,
			);

			$results = $this->model_project_project_customer->getVendorChecklists($filter_data);
			// var_dump($results);exit;
			foreach ($results as $result) {
				$json[] = array(
					'project_customer_id' => $result['project_customer_id'],
					'company_eng_name'        => strip_tags(html_entity_decode($result['company_eng_name'], ENT_QUOTES, 'UTF-8')),
					'company_chn_name'        => strip_tags(html_entity_decode($result['company_chn_name'], ENT_QUOTES, 'UTF-8'))
				);
			}
		}



		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

	public function file_upload(){
		$data['token'] = $this->session->data['token'];
		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');
		$this->response->setOutput($this->load->view('project/project_upload2.tpl', $data));
	}
	public function getFiles(){
		$this->load->model('project/project_progress');
		// $file_ids = ($this->request->post['da'])?$this->request->post['da']：'';
		// $file_id = implode(",",$file_ids);
		if(!empty($this->request->post['da'])){
			$file_id = implode(",",$this->request->post['da']);
			$result = $this->model_project_project_progress->getFiles($file_id);
			if($result){
				$json = array(
					'status' => 1,
					'msg' => 'success',
					'project_file' => $result,
				);
			}else{
				$json = array(
					'status' =>0,
					'msg' => 'error',
				);
			}
		}else{
			$json = array(
					'status' => 2,
					'msg' => 'error',
				);
		}
		$this->response->addHeader('Content-Type: application/json');
		echo json_encode($json);
	}
	public function uploads() {
		$this->language->load('project/project_progress');
		$this->load->model('project/project_progress');

		$json = array();

		// Check user has permission
		if (!$this->user->hasPermission('modify', 'project/project_progress')) {
			$json['error'] = $this->language->get('error_permission');
		}

		// Make sure we have the correct directory
		if (isset($this->request->get['directory'])) {
			$directory = rtrim(DIR_PROJECT . 'project_upload2/' . str_replace(array('../', '..\\', '..'), '', $this->request->get['directory']), '/');
		} else {
			$directory = DIR_PROJECT . 'project_upload2';
		}

		// Check its a directory
		if (!is_dir($directory)) {
			$json['error'] = $this->language->get('error_directory');
		}

		if (!$json) {
			if (!empty($this->request->files['file']['name']) && is_file($this->request->files['file']['tmp_name'])) {
				// Sanitize the filename
				$filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8'));

				// Validate the filename length
				if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 128)) {
					$json['error'] = $this->language->get('error_filename');
				}

				// Allowed file extension types
				$allowed = array();
				$FileTypeExt = "png
jpe
jpeg
jpg
pdf
xls
xlsx
docx
doc";
				$FileTypeMime = "image/png
image/jpeg
application/msword
application/vnd.ms-excel
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/pdf";
				$extension_allowed = preg_replace('~\r?\n~', "\n", $FileTypeExt);

				$filetypes = explode("\n", $extension_allowed);

				foreach ($filetypes as $filetype) {
					$allowed[] = trim($filetype);
				}

				if (!in_array(strtolower(substr(strrchr($filename, '.'), 1)), $allowed)) {
					$json['error'] = $this->language->get('error_filetype');
				}
				// var_dump($this->config->get('config_file_mime_allowed'));exit;
				// Allowed file mime types
				$allowed = array();

				$mime_allowed = preg_replace('~\r?\n~', "\n", $FileTypeMime);

				$filetypes = explode("\n", $mime_allowed);

				foreach ($filetypes as $filetype) {
					$allowed[] = trim($filetype);
				}

				if (!in_array($this->request->files['file']['type'], $allowed)) {
					$json['error'] = $this->language->get('error_filetype');
				}

				// Check to see if any PHP files are trying to be uploaded
				$content = file_get_contents($this->request->files['file']['tmp_name']);

				if (preg_match('/\<\?php/i', $content)) {
					$json['error'] = $this->language->get('error_filetype');
				}

				// Return any upload error
				if ($this->request->files['file']['error'] != UPLOAD_ERR_OK) {
					$json['error'] = $this->language->get('error_upload_' . $this->request->files['file']['error']);
				}
			} else {
				$json['error'] = $this->language->get('error_upload');
			}
		}

		if (!$json) {
			$file = token(16).'_'.$this->request->files['file']['name'];
			$uploadfile = $directory . '/' .$file;
			move_uploaded_file($this->request->files['file']['tmp_name'], $uploadfile);
			$project_file_id = $this->model_project_project_progress->addFile($filename,$uploadfile);
			$json['filename'] = $file;
			$json['mask'] = $filename;
			$json['uploadfile'] = $uploadfile;
			$json['project_file_id'] = $project_file_id;
			$json['tmp_name'] = $this->request->files['file']['tmp_name'];
			$json['code'] = 0;
			$json['success'] = $this->language->get('text_upload');
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

		public function clear() {
		$this->load->language('project/project_customer');
		$this->load->model('project/project_customer');

		$json = array();

		if (!$this->user->hasPermission('modify', 'project/project_customer')) {
			$json['error'] = $this->language->get('error_permission');
		}

		if (!$json) {

			$file = $this->request->request['name'];
			
			if(!empty($file)){
				$project_customer_id = isset($this->request->request['project_customer_id'])?$this->request->request['project_customer_id']:null;
				$query	= $this->model_project_project_customer->clearFile($project_customer_id,$this->request->request['id'],$this->request->request['name']);
				 // var_dump($query);
				
				
				if (is_file($query)) {
					unlink($query);
				} elseif (is_dir($query)) {
					rmdir($query);
				}

				$json['success'] = $this->language->get('text_clear');
			}else{
				$json['msg'] = 'No File!';
			}
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

}
